package cn.ctbu.xiaopeng.studentscoremanage.utils;

import cn.ctbu.xiaopeng.studentscoremanage.pojo.vo.page.SortParam;
import org.springframework.data.domain.Sort;

import java.util.ArrayList;
import java.util.List;

/**
 * 构建表头排序的工具类
 * @author xiaopeng
 * @version 1.0
 */
public class BuildTbSortUtils {
    /**
     * 根据前端传入的多个排序参数构建Sort对象
     *
     * @param sorts 前端传入的多个排序参数，
     *              格式如：[{"field":"id","type":"ASC"},{"field":"age","type":"DESC"}]
     * @return Sort对象
     */
    public static Sort buildSort(List<SortParam> sorts) {
        List<Sort.Order> orders = new ArrayList<>();
        for (SortParam param : sorts) {
            Sort.Direction direction = param.getType()
                    .equalsIgnoreCase("ASC") ? Sort.Direction.ASC : Sort.Direction.DESC;
            orders.add(new Sort.Order(direction, param.getField()));
        }
        return Sort.by(orders);
    }
}
